package com.hanyxx.thread;

import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;

/**
 * @author layman
 * @date 2021/2/7
 */
public class Demo10 {
    public static void main(String[] args) {
        Cook cook = new Cook();
        WaiterA threadA = new WaiterA(cook);
        threadA.start();
        WaiterB threadB = new WaiterB(cook);
        threadB.start();
    }
}
class Cook {
    //强制线程从公共堆栈获取数据
    public volatile List<Integer> meal = new ArrayList();
    public void add() throws InterruptedException {
        for (int i = 1; i < 8 ; i++) {
            meal.add(i);
            System.out.println("第 "+i+" 道菜做好了");
            Thread.sleep(1000);
        }
    }
}
class WaiterA extends  Thread{
    private Cook cook;
    public WaiterA(Cook cook){
        this.cook = cook;
    }
    @SneakyThrows
    @Override
    public void run() {
        while (true){
            cook.add();
        }
    }
}
class WaiterB extends  Thread{
    private Cook cook;
    public WaiterB(Cook cook){
        this.cook = cook;
    }
    @SneakyThrows
    @Override
    public void run() {
        while (true){
            if(cook.meal.size() == 5){
                System.out.println("1号桌5个菜已经做好了，准备上菜");
                throw new InterruptedException();
            }
        }
    }
}